import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Generated with window builder with a few edits <br />
 * Screen for formating from bibTex part.
 * @author cerko, mydlo
 *
 */
public class Frontend {
	
	private static int windowWidth = 800;
	private static int windowHeight = 630;
	private static String backgroundPath = "img/screen/addReferenceScreen.jpg";
	
	public static final boolean DEBUG = false;

	private JFrame frame;
	private JTextField TeXInputField;
	private JTextField biBField;
	private JTextField teXOutputField;
	private JTextField formatOutputField;
	
	private File texInput;
	private File texOutput;
	private File bib;
	private File formatOutput;
	private File formatInput;
	private JButton btnStart;
	private JPanel backgroundPanel;
	
	private static JFrame home;
	private JFrame fromBibtexView;
	private JFrame reformatTexView;
	private JFrame saveIntoBibView;
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Frontend window = new Frontend();
					window.frame.setVisible(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Frontend() {
		initialize();
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		frame.setSize(windowWidth, windowHeight);

		GraphicsConfiguration gc = frame.getGraphicsConfiguration();
		Rectangle bounds = gc.getBounds();

		frame.setLocation(
				(int) ((bounds.width / 2) - (windowWidth / 2)),
				(int) ((bounds.height / 2) - (windowHeight / 2))
				);
		
		home = new BasicView(this, frame);
		
		//test svn
		//magic to make background work
		backgroundPanel = new JPanel(){
			@Override
			 public void paintComponent(Graphics g){
				Image img = null;
				try {
					img = ImageIO.read(new File(backgroundPath));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                g.drawImage(img, 0, 0, null);
            } 
		};
		
		
		
		frame.getContentPane().setLayout(createFromBibTexView());
	}
	
	/**
	 * setne frame na visible
	 */
	public void setFirstModeView(){
		frame.setVisible(true);
	}
	
	/**
	 * vytvory cely layout
	 * @return hotovy layout
	 */
	public GroupLayout createFromBibTexView(){
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(backgroundPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(backgroundPanel, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
		);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 794, 591);
		//nastavenie priesvitneho panelu
		panel.setOpaque(false);
		
		biBField = new JTextField();
		biBField.setBounds(310, 362, 177, 14);
		biBField.setColumns(10);
		biBField.setOpaque(false);
		biBField.setBorder(BorderFactory.createEmptyBorder());
		
		TeXInputField = new JTextField();
		TeXInputField.setBounds(310, 318, 177, 14);
		TeXInputField.setColumns(10);
		TeXInputField.setOpaque(false);
		TeXInputField.setBorder(BorderFactory.createEmptyBorder());
		
		JButton btnTeXInput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnTeXInput.setBounds(494, 310, 85, 30);
		btnTeXInput.setBorder(BorderFactory.createEmptyBorder());
		btnTeXInput.setOpaque(false);
		btnTeXInput.setContentAreaFilled(false);
		
		btnTeXInput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnTeXInput));
		
		JButton btnBiBInput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnBiBInput.setBounds(494, 354, 85, 30);
		btnBiBInput.setBorder(BorderFactory.createEmptyBorder());
		btnBiBInput.setOpaque(false);
		btnBiBInput.setContentAreaFilled(false);
		
		btnBiBInput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnBiBInput));
		
		btnBiBInput.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("bibTex files (.bib)", "bib"));
				int returnVal = fileChooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					bib = fileChooser.getSelectedFile();
					biBField.setText(bib.getAbsolutePath());
				}
			}
		});
		
		teXOutputField = new JTextField();
		teXOutputField.setBounds(310, 403, 177, 14);
		teXOutputField.setColumns(10);
		teXOutputField.setOpaque(false);
		teXOutputField.setBorder(BorderFactory.createEmptyBorder());
		JButton btnTexOutput =  new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnTexOutput.setBounds(494, 395, 85, 30);
		btnTexOutput.setBorder(BorderFactory.createEmptyBorder());
		btnTexOutput.setOpaque(false);
		btnTexOutput.setContentAreaFilled(false);
		
		btnTexOutput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnTexOutput));
		
		btnTexOutput.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("TeX file (.tex)", "tex"));
				int returnVal = fileChooser.showSaveDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					teXOutputField.setText(fileChooser.getSelectedFile().getAbsolutePath());
					if (!teXOutputField.getText().endsWith(".tex")){
						teXOutputField.setText(teXOutputField.getText() + ".tex");
					}
				}
			}
		});
		
		btnStart = new JButton(new ImageIcon("img/buttons/startButton.png"));
		btnStart.setBounds(350, 435, 102, 102);
		btnStart.setBorder(BorderFactory.createEmptyBorder());
		btnStart.setOpaque(false);
		btnStart.setContentAreaFilled(false);
		
		btnStart.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/startButtonA.png"), new ImageIcon("img/buttons/startButton.png"), btnStart));
		
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO odpal
				boolean removeMe = false;
				try {
					texOutput = new File(teXOutputField.getText());
					new Dispatcher().insertRefs(texInput, texOutput, bib, formatOutput);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		backgroundPanel.setLayout(null);
		
		formatOutputField = new JTextField();
		formatOutputField.setBounds(310, 275, 177, 14);
		formatOutputField.setColumns(10);
		formatOutputField.setOpaque(false);
		formatOutputField.setBorder(BorderFactory.createEmptyBorder());
		
		JButton  btnFormatinput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnFormatinput.setBounds(494, 267, 85, 30);
		btnFormatinput.setBorder(BorderFactory.createEmptyBorder());
		btnFormatinput.setOpaque(false);
		btnFormatinput.setContentAreaFilled(false);
		
		btnFormatinput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnFormatinput));
		
		btnFormatinput.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("txt file (.txt)", "txt"));
				int returnVal = fileChooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					formatOutput = fileChooser.getSelectedFile();
					formatOutputField.setText(formatOutput.getAbsolutePath());
				}
			}
		});
		
		btnTeXInput.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("tex files (.tex)", "tex"));
				int returnVal = fileChooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					texInput = fileChooser.getSelectedFile();
					TeXInputField.setText(texInput.getAbsolutePath());
				}
			}
		});
		
		JButton homeButton = new JButton(new ImageIcon("img/buttons/homeButton.png"));
		
		homeButton.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/homeButtonA.png"), new ImageIcon("img/buttons/homeButton.png"), homeButton));
		
		homeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
				Frontend.home.setVisible(true);
			}
		});
		homeButton.setBounds(38, 513, 52, 52);
		homeButton.setBorder(BorderFactory.createEmptyBorder());
		homeButton.setOpaque(false);
		homeButton.setContentAreaFilled(false);
		
		backgroundPanel.add(panel);
		panel.setLayout(null);
		panel.add(biBField);
		panel.add(TeXInputField);
		panel.add(teXOutputField);
		panel.add(formatOutputField);
		panel.add(btnFormatinput);
		panel.add(btnTexOutput);
		panel.add(btnBiBInput);
		panel.add(btnTeXInput);
		panel.add(btnStart);
		panel.add(homeButton);
		
		return groupLayout;
	}

	public void setBasicView(JFrame basicView) {
		Frontend.home = basicView;
	}
}
